<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ item.pubdate | moment }}</span>
            <van-button size="mini" type="default" @click="showFn(item)"
              >回复 ({{ item.reply_count }})</van-button
            >
          </p>
        </div>
        <van-icon
          slot="right-icon"
          :name="item.is_liking ? 'like' : 'like-o'"
          @click="changelike(item)"
        />
      </van-cell>
    </van-list>
    <!-- /评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        placeholder="请输入评论内容"
        v-model.trim="commentvalue"
      >
        <van-button slot="button" size="mini" type="info" @click="addcomment"
          >发布</van-button
        >
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->

    <van-action-sheet v-model="show" title="评论回复">
      <reply
        v-if="show"
        :currentCommon="currentCommon"
        :artid="artid"
        @addreplaynum="addreplaynumFn"
      ></reply>
    </van-action-sheet>
  </div>
</template>

<script>
import {
  getarticelcomment,
  addlikes,
  removelikes,
  addarticelcomment
} from '@/api/article'

import Reply from './reply.vue'
export default {
  name: 'ArticleComment',
  props: ['articel'],
  components: {
    Reply
  },
  data() {
    return {
      currentCommon: '',
      show: false,
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: '',
      commentvalue: ''
    }
  },

  methods: {
    // 回复评论后回复数量加一
    addreplaynumFn() {
      this.currentCommon.reply_count++
    },
    // 控制回复菜单的隐藏和显示
    async showFn(item) {
      this.show = true
      this.currentCommon = item
    },
    // 新增某文章评论
    async addcomment() {
      if (this.commentvalue === '') {
        return this.$toast('内容不能为空')
      }
      const res = await addarticelcomment(this.artid, this.commentvalue)
      this.list.unshift(res.data.new_obj)
      this.commentvalue = ''
      this.$toast('评论成功')
    },
    // 获取文章评论
    async onLoad() {
      // 异步更新数据
      const res = await getarticelcomment(this.artid, this.offset)
      this.list = [...this.list, ...res.data.results]
      this.offset = res.data.last_id

      this.loading = false
      if (this.list.length >= 10) {
        this.finished = true
      }
    },
    // 改变评论点赞状态
    async changelike(item) {
      if (item.is_liking) {
        await removelikes(item.com_id)
        item.is_liking = !item.is_liking
      } else {
        await addlikes(item.com_id)
        item.is_liking = !item.is_liking
      }
    }
  },
  computed: {
    artid() {
      return this.$route.params.id
    }
  }
}
</script>

<style scoped lang="less">
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
